package com.songmao.pricecloudapitest.testcase.usermanagement;

import com.songmao.pricecloudapitest.requestcore.ResponePoJo;
import com.songmao.pricecloudapitest.server.PriceCloudHttpCore;
import com.songmao.pricecloudapitest.utils.AssertCore;
import com.songmao.pricecloudapitest.utils.ParsingJson;
import com.songmao.pricecloudapitest.utils.TestData;
import com.songmao.pricecloudapitest.utils.TestUitl;
import org.testng.annotations.Test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author Remy
 * @description : 报价用户列表相关测试用例
 * @date 2020-06-28 11:50
 */
public class CustomerListTestCase extends PriceCloudHttpCore {

    @Test(dataProvider = "dataProviderMap", dataProviderClass = TestData.class, description = "客户列表查询测试")
    public void customerListTest(Map<String, String> map) {
        ResponePoJo pojo = get(map.get("url"));
        //判断响应状态是否等于200
        AssertCore.assertEquals(Integer.parseInt(map.get("expected")), pojo.getStatus());
    }

    @Test(dataProvider = "dataProviderMap", dataProviderClass = TestData.class, description = "查询状态已启用的用户")
    public void findUserStatusTestCase(Map<String, String> map) {
        ResponePoJo pojo = get(map.get("url"));
        //判断响应状态是否等于200
        AssertCore.assertEquals(200, pojo.getStatus());
        //判断接口返回的enabled:1
        List<Object> list = ParsingJson.getPathCodes(pojo.getData().toString(), map.get("expectedPath"));
        AssertCore.assertTrue(Optional.ofNullable(list).isPresent(), "接口返回的用户集合为空");
        assert list != null;
        for (Object obj : list) {
            AssertCore.assertEquals(map.get("expected"), obj.toString());
        }
    }

    @Test(dataProvider = "dataProviderMap", dataProviderClass = TestData.class, description = "根据时间筛选用户")
    public void filterUsersBasedOnTime(Map<String, String> map) throws ParseException {
        ResponePoJo pojo = super.get(map.get("url"));
        AssertCore.assertEquals(200, pojo.getStatus());
        List<Object> userList = ParsingJson.getPathCodes(pojo.getData().toString(), map.get("expectedPath"));
        Map<String, String> expectedMap = TestUitl.splitString(map.get("expected"));

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        assert expectedMap != null;
        Date startTime = sdf.parse(expectedMap.get("startTime"));
        Date end = sdf.parse(expectedMap.get("endTime"));
        assert userList != null;
        for (Object obj : userList) {
            AssertCore.assertTrue(TestUitl.judgingTimePeriod(startTime, end, sdf.parse(obj.toString())));
        }

    }

    @Test(dataProvider = "dataProviderMap", dataProviderClass = TestData.class, description = "测试接口正常参数返回得page")
    public void customerListPageTest(Map<String, String> map) {
        ResponePoJo pojo = get(map.get("url"));
        AssertCore.assertEquals(200, pojo.getStatus());
        List<Object> userList = ParsingJson.getPathCodes(pojo.getData().toString(), map.get("expectedPath"));
        AssertCore.assertNotNull(userList, "返回得用户列表为null");
        assert userList != null;
        AssertCore.assertTrue(Integer.parseInt(map.get("expected")) == userList.size());
    }

    @Test(dataProvider = "dataProviderMap", dataProviderClass = TestData.class, description = "测试参数传入-1,9999等最大值接口的返回值")
    public void customerListMaxPageTest(Map<String, String> map) {
        Map<String, String> expectedMap = TestUitl.splitString(map.get("expectedPath"));
        ResponePoJo pojo = get(map.get("url"));
        AssertCore.assertEquals(200, pojo.getStatus());
        assert expectedMap != null;
        List<Object> userList = ParsingJson.getPathCodes(pojo.getData().toString(), expectedMap.get("appid"));
        String totalCounts = Objects.requireNonNull(ParsingJson.getPathCode(pojo.getData().toString(), expectedMap.get("totalCounts"))).toString();
        AssertCore.assertNotNull(userList, "返回得用户列表为null");
        assert userList != null;
        AssertCore.assertTrue(userList.size() == Integer.parseInt(totalCounts));
    }


}
